package com.java.demo.rocketmq;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhangyunzhe
 */
@Component("ProducerService")
public class ProducerService {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 默认：无回调消息
     */
    public void sendMessage(){
        for (int i = 0; i < 1000; i++) {
            rocketMQTemplate.convertAndSend("216-topic-zhe","3点几拉，该喝茶了喂"+i);
            rocketMQTemplate.sendOneWay("216-topic-zhe","3点几拉，该喝茶了喂"+i);
        }
    }

    /**
     * 模式：同步消息
     */
    public void sendSyncMessage(){
        for (int i = 0; i < 10; i++) {
            SendResult sendResult = rocketMQTemplate.syncSend("217-topic-zhe", "同步消息"+i);
            System.out.println(sendResult);
        }
    }

    /**
     * 模式：同步顺序消息
     * topic、message、queue
     */
    public void sendOrderMessage(){
        rocketMQTemplate.syncSendOrderly("217-topic-order","2022217同步顺序消息发送","2022217");
        rocketMQTemplate.syncSendOrderly("217-topic-order","2022217同步顺序消息接受","2022217");
        rocketMQTemplate.syncSendOrderly("217-topic-order","2022217同步顺序消息消费","2022217");
    }
}
